Open
Conversation
CheezItMan
reviewed
Nov 20, 2022
CheezItMan
left a comment
There was a problem hiding this comment.
Nice work Tanil & Stacy, you hit the learning goals here. Nice work testing and writing validations. I left some minor comments. Let me know if you have questions.
Comment on lines
+11
to
+31
| def to_dict(self): | ||
| return({ | ||
| "id": self.id, | ||
| "name": self.name, | ||
| "color": self.color, | ||
| "description": self.description | ||
| }) | ||
| @classmethod | ||
| def from_json(cls, req_body): | ||
| return cls( | ||
| name = req_body["name"], | ||
| color = req_body["color"], | ||
| description = req_body["description"]) | ||
|
|
||
| def update(self, req_body): | ||
| try: | ||
| self.name = req_body["name"] | ||
| self.color = req_body["color"] | ||
| self.description = req_body["description"] | ||
| except KeyError as error: | ||
| raise error |
| planets_bp = Blueprint("planets_bp", __name__, url_prefix = "/planets") | ||
|
|
||
| # HELPER FUNCTION | ||
| def validate_id(class_obj, id): |
| # CREATE RESOURCE | ||
| @planets_bp.route("", methods = ["POST"]) | ||
| def create_planet(): | ||
| request_body = request.get_json() |
There was a problem hiding this comment.
Doing some validation on the request body here would be nice, just to check if it has the required fields.
Comment on lines
+69
to
+72
| try: | ||
| planet.update(request_body) | ||
| except KeyError as error: | ||
| return make_response({'message': f"Missing attribute: {error}"}, 400) |
There was a problem hiding this comment.
This is good validation, something similar could be done in the post request. It should also be tested
Comment on lines
+43
to
+51
| assert response_body[0]["name"] == "foo1" | ||
| assert response_body[1]["name"] == "foo2" | ||
| assert response_body[2]["name"] == "foo3" | ||
| assert response_body[0]["color"] == "bar1" | ||
| assert response_body[1]["color"] == "bar2" | ||
| assert response_body[2]["color"] == "bar3" | ||
| assert response_body[0]["description"] == "foobar1" | ||
| assert response_body[1]["description"] == "foobar2" | ||
| assert response_body[2]["description"] == "foobar3" |
There was a problem hiding this comment.
You could also do something like:
Suggested change
| assert response_body[0]["name"] == "foo1" | |
| assert response_body[1]["name"] == "foo2" | |
| assert response_body[2]["name"] == "foo3" | |
| assert response_body[0]["color"] == "bar1" | |
| assert response_body[1]["color"] == "bar2" | |
| assert response_body[2]["color"] == "bar3" | |
| assert response_body[0]["description"] == "foobar1" | |
| assert response_body[1]["description"] == "foobar2" | |
| assert response_body[2]["description"] == "foobar3" | |
| assert response_body == [ { | |
| "name": "foo1", | |
| "color": "bar1", | |
| "description": "foobar1", | |
| }, | |
| { | |
| "name": "foo2", | |
| "color": "bar2", | |
| "description": "foobar2", | |
| } | |
| ... |
|
|
||
|
|
||
|
|
||
| def test_create_planet_can_create_planet_in_empty_db(client): |
There was a problem hiding this comment.
You should also test an invalid create planet request.
|
|
||
| # DELETE RESOURCE | ||
| @planets_bp.route("/<planet_id>", methods = ["DELETE"]) | ||
| def delete_planet(planet_id): |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
solar-system-api part 1